<template>
  <el-dialog :visible="showDialog" title="新增员工" @close="btnCancel">
    <!-- 表单 -->
    <el-form ref="employeeFormRef" label-width="120px" :model="employeeForm" :rules="employeeFormRules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="employeeForm.username" style="width:50%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="employeeForm.mobile" style="width:50%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="employeeForm.timeOfEntry" style="width:50%" placeholder="请选择日期" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="employeeForm.formOfEmployment" style="width:50%" placeholder="请选择">
          <!-- select的选项用el-option -->
          <el-option v-for="(item,index) in EmployeeEnum.hireType" :key="index" :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="employeeForm.workNumber" style="width:50%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="employeeForm.departmentName" style="width:50%" placeholder="请选择部门" @focus="getDepartments" />
        <!-- 树形组件 -->
        <el-tree v-if="showTree" v-loading="loading" :data="treeData" :props="{label:'name'}" default-expand-all @node-click="selectNode" />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="employeeForm.correctionTime" style="width:50%" placeholder="请选择日期" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="btnCancel">取消</el-button>
          <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { addEmployee } from '@/api/employees'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index.js'
import EmployeeEnum from '@/api/constant/employees'
export default {
  name: 'HrSassAddEmployee',
  props: {
    showDialog: {
      default: false,
      type: Boolean
    }
  },

  data() {
    return {
      treeData: [], // 定义数组接收树形数据
      showTree: false, // 控制树形的显示或者隐藏
      loading: false, // 控制树的显示或者隐藏进度条
      // 表单数据
      employeeForm: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      employeeFormRules: {
        username: [{ required: true, message: '用户姓名不能为空', trigger: 'blur' }, {
          min: 1, max: 4, message: '用户姓名为1-4位'
        }],
        mobile: [{ required: true, message: '手机号不能为空', trigger: 'blur' }, {
          pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur'
        }],
        formOfEmployment: [{ required: true, message: '聘用形式不能为空', trigger: 'blur' }],
        workNumber: [{ required: true, message: '工号不能为空', trigger: 'blur' }],
        departmentName: [{ required: true, message: '部门不能为空', trigger: 'change' }],
        timeOfEntry: [{ required: true, message: '入职时间', trigger: 'blur' }]
      },
      EmployeeEnum
    }
  },
  mounted() {

  },

  methods: {
    async getDepartments() {
      this.showTree = true
      this.loading = true
      const { depts } = await getDepartments()
      // depts是数组 但不是树形
      this.treeData = tranListToTreeData(depts, '')
      this.loading = false
    },
    // 树形结构节点被点击时的回调函数
    // 具体用法看element-ui tree结构node-click事件
    selectNode(node) {
      this.employeeForm.departmentName = node.name
      // 隐藏树木
      this.showTree = false
    },
    async btnOK() {
      // 较验表单
      try {
        await this.$refs.employeeFormRef.validate()
        // 较验成功
        // 调用新增员工接口
        await addEmployee(this.employeeForm)
        // 通知父组件 重新渲染
        // this.$parent  当前父组件的实例
        // console.log(this.$parent)
        // 先判断父组件有没有该方法
        this.$parent.LoadEmployeeList && this.$parent.LoadEmployeeList()// 直接调用父组件的更新方法
        this.$parent.showDialog = false// 同样是父组件的方法
        // 此方法得注意子组件的位置
        this.$message.success('新增员工成功')
        // 点击确定后之所以没有重置数据,是因为点击确定后会关闭弹出层 此时会触发弹出层的close事件 而close事件又绑定着btnCancel方法,相当于间接的调用了btnCancel方法
      } catch (error) {
        this.$message.error('较验失败')
      }
    },
    btnCancel() {
      // 清空数据
      this.employeeForm = { username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: '' }
      // 重置表单 移除之前的较验
      this.$refs.employeeFormRef.resetFields()
      //  关闭弹层
      this.$emit('update:showDialog', false)
      // 此处不用自定义事件
      // 用此方法 update:props名称 这么写的话 可以在父组件 直接用sync修饰符处理
    }

  }
}
</script>

<style lang="scss" scoped>

</style>
